package com.leoren.run.activity.announcement;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.leoren.run.R;
import com.leoren.run.common.Const;
import com.leoren.run.common.UrlConstants;
import com.leoren.run.dbbean.UserDB;
import com.leoren.run.log.L;
import com.leoren.run.utils.GsonUtil;
import com.leoren.run.utils.Http.OkHttp.CommonOkHttpClient;
import com.leoren.run.utils.Http.OkHttp.exception.OkHttpException;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataHandle;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataListener;
import com.leoren.run.utils.Http.OkHttp.request.CommonRequest;
import com.leoren.run.vo.AnnounceVo;

import org.litepal.LitePal;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 显示具体系统公布的界面
 */
public class AnnounceActivity extends AppCompatActivity {

    private Toolbar toolbar;

    private AnnounceVo announceVo;

    private TextView titleView;
    private TextView dateText;
    private TextView contentText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announce);

        initLayout();
    }


    private void initLayout(){
        toolbar = findViewById(R.id.announce_toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AnnounceActivity.this.finish();
            }
        });

        titleView = findViewById(R.id.announce_title);
        dateText = findViewById(R.id.announce_time);
        contentText = findViewById(R.id.announce_content);

        initData();
    }

    /**
     * 从上一个界面得到公布数据并展示
     */
    private void initData(){
        Intent intent = getIntent();
        String str = intent.getStringExtra(Const.ANNOUNCE);
        announceVo = GsonUtil.formatJsonToAnnounceVo(str);
        L.e("阅读界面 " + announceVo.toString());
        titleView.setText(announceVo.getTitle());
        contentText.setText(announceVo.getContent());
        dateText.setText(announceVo.getDate());

        readAnnounce();
    }

    /**
     * 将该条公布设为已读状态
     */
    private void readAnnounce(){
        UserDB userDB = LitePal.findFirst(UserDB.class);
        Long announceId = announceVo.getId();
        L.e(announceId + "hahahahhahahaklkkk");
        RequestBody body = new FormBody.Builder()
                .add("userId", String.valueOf(userDB.getTagId()))
                .add("mentId", String.valueOf(announceId))
                .build();
        Request request = CommonRequest.createPostRequest(UrlConstants.READ_ANNOUNCE, body);
        CommonOkHttpClient.post(request, new DisposeDataHandle(new DisposeDataListener() {
            @Override
            public void onSuccess(String responseStr) {
                L.e(responseStr);
            }

            @Override
            public void onFailure(OkHttpException e) {

            }
        }));
    }


}
